package org.administer.contaroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.administer.dao.CrmRoleDao;
import org.administer.pojo.CrmRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private CrmRoleDao crmRoleDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "role/roleindex";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmRole> pager= PageHelper.startPage(page,limit);
        List<CrmRole> list=crmRoleDao.getAll();
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer id) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmRoleDao.del(id);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/form")
    public String form() throws Exception{
        return "role/updateform";
    }

    @RequestMapping("/addform")
    public String addform() throws Exception{
        return "role/addform";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(CrmRole crmRole) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmRoleDao.add(crmRole);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/view")
    @ResponseBody
    public CrmRole view(Integer id) throws Exception{
         return  crmRoleDao.get(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(CrmRole crmRole) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmRoleDao.update(crmRole);
        map.put("result",true);
        return map;
    }
    @RequestMapping("/rolePers")
    @ResponseBody
    public List<Long> rolePers(Integer id)throws Exception{
        return crmRoleDao.getPerIdsByRoleId(id);
    }

    @RequestMapping("/shouquan")
    @ResponseBody
    public Map<String,Object> shouquan(Integer roleId,String persIds) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmRoleDao.deleteRolePermissions(roleId);
        crmRoleDao.addRolePermissions(roleId,persIds.split(","));
        return map;
    }
    @RequestMapping("/getRole")
    @ResponseBody
    public CrmRole getByUserName(String userName,Integer id) throws Exception{
        return crmRoleDao.getrole(userName,id);
    }
}
